package com.taoism.utils;

import java.io.*;

/**
 * 图片上传工具类
 * lhw
 * 2017-12-26
 */
public class UploadImageUtils {

    /**
     * 上传图片
     *
     * @param upload
     * @param uploadFileName
     * @param path
     * @throws IOException
     */
    public void uploadImage(File upload, String uploadFileName, String path) throws IOException {
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(upload);
            File f = new File(path);
            // 判断 f 文件是否存在
            if (!f.exists()) {
                f.mkdirs();
            }
            os = new FileOutputStream(path + "/" + uploadFileName);
            byte[] buffer = new byte[1024];
            int count = 0;
            while ((count = is.read(buffer)) > 0) {
                os.write(buffer, 0, count);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            File f = new File(path + "/" + uploadFileName);
            if (f.exists()) {
                f.delete();
            }
        } finally {
            os.close();
            is.close();
        }
    }

}
